package avicit.bdp.dcs.datax;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class DataxTest {

    public static void main(String[] args) {
        try {
            String json = "D:\\2020\\bigdata\\dataX\\datax\\job\\stream2stream.json -1";

            Process process = Runtime.getRuntime().exec("cmd");   // 获取控制台
            new Thread(() -> {
                try {
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));  // 获取输入流
                    bw.write("CHCP 65001");
                    bw.newLine();

                    bw.write("d:");
                    bw.newLine();

                    bw.write("cd D:\\2020\\bigdata\\dataX\\datax\\bin");
                    bw.newLine(); // 因为读取时是一行行读取的，不加newline无法继续往下读

                    bw.write("python datax.py "+ json);
                    bw.newLine();

                    bw.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();

            new Thread() {
                public void run() {
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                        String cmdout = "";
                        while ((cmdout = br.readLine()) != null) {
                            System.out.println(cmdout);
                        }

                        process.destroy();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
